<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends BaseController {
    public function index(){
    	$where = array();
        if (I('key')) {
            $where['title'] = array('like','%'.I('key').'%');
        }
    	$this->list = M("News")->Where($where)->Where($map)->order('addtime desc')->Select();
		$this->display();
    }
    public function release(){
		if (!IS_POST) {
        	$this->display();
        }
        if (IS_POST) {
         	$model = D('News');
         	if(!$model->create()){
                exit($model->getError());
         	}else{         		
                if ($model->add()) {
                    $this->success("添加成功", U('news/index'),1);
                } else {
                    $this->error("添加失败");
                }
         	}
        }
    }
    public function editor($id){
		if (!IS_POST) {
			$model = M('News');
        	$map['id'] = $id;
    		$info = $model->where($map)->find();
        	$this->assign('info',$info);
        	$this->display();
        }
        if (IS_POST) {
         	$model = D('News');
         	if($model->create()){
         		if ($model->save()) {
                    $this->success("修改成功", U('news/index'),1);
                } else {
                    $this->error("修改失败");
                }
         	}else{
         		$this->error($model->getError());
                exit();
         	}
        }
    }
    public function delete($id){
		if (!empty($id)) {
            $model = M("News");
            $result = $model->delete($id);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！');
            }
        } else {
            $this->error('编号错误！');
        }
    }
    public function state($id){
		$model = M("News");
        //查询该条信息的state字段值
        $result = $model->find($id);
        //更新字段
        $data['id']=$id;
        if($result['state'] == 1){
          $data['state']=0;
        }
        if($result['state'] == 0){
          $data['state']=1;
        }
        if($model->save($data)){       	
            $this->success('状态更新成功!',U('news/index'),1);
        }else{
            $this->error('状态更新失败',U('news/index'),1);
        }
    }
}